{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#解析详细数据\n",
    "\n",
    "url='/live/flight/CCA104/history/20180613/0450Z/VHHH/ZBTJ'\n",
    "url= 'https://zh.flightaware.com' +url\n",
    "headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'}\n",
    "\n",
    "import requests\n",
    "\n",
    "r= requests.get(url)\n",
    "html= r.text\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "219946"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html.find('var trackpollBootstrap')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arrived\n",
      "{'scheduled': 1528865400, 'estimated': 1528870140, 'actual': 1528870140}\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "#解析飞行数据\n",
    "import json\n",
    "start= html.find('var trackpollBootstrap')+len('var trackpollBootstrap')+3\n",
    "end = html[start:].find('</script>')\n",
    "raw_data= html[start: start+end-1]\n",
    "json_data= json.loads(raw_data)\n",
    "#print( json_data)\n",
    "for k in json_data['flights'].keys():\n",
    "    flight_data= json_data['flights'][k]\n",
    "    #print(flight_data['cancelled'])\n",
    "    print(flight_data['flightStatus'])\n",
    "    print(flight_data['takeoffTimes'])\n",
    "    print(flight_data['takeoffTimes']['actual'] - flight_data['takeoffTimes']['estimated'] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cancelled': False,\n",
       " 'landing_a': 1518850380,\n",
       " 'landing_e': 1518851760,\n",
       " 'landing_s': 1518848820,\n",
       " 'raw': '{\"version\":\"2.0\",\"summary\":false,\"flights\":{\"CCA1219-1518672300-schedule-0001\":{\"activityLog\":{\"flights\":[{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":null,\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A321\",\"aircraftTypeFriendly\":\"Airbus A321 (双发)\",\"flightId\":\"CCA1219-1528953000-schedule-0002\",\"takeoffTimes\":{\"scheduled\":1529125800,\"estimated\":1529126400,\"actual\":null},\"landingTimes\":{\"scheduled\":1529132400,\"estimated\":1529133000,\"actual\":null},\"gateDepartureTimes\":{\"scheduled\":1529125800,\"estimated\":null,\"actual\":null},\"gateArrivalTimes\":{\"scheduled\":1529133600,\"estimated\":null,\"actual\":null},\"ga\":false,\"flightStatus\":\"\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180616/0510Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":null,\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A321\",\"aircraftTypeFriendly\":\"Airbus A321 (双发)\",\"flightId\":\"CCA1219-1528866600-schedule-0002\",\"takeoffTimes\":{\"scheduled\":1529039400,\"estimated\":1529040000,\"actual\":null},\"landingTimes\":{\"scheduled\":1529046000,\"estimated\":1529046600,\"actual\":null},\"gateDepartureTimes\":{\"scheduled\":1529039400,\"estimated\":null,\"actual\":null},\"gateArrivalTimes\":{\"scheduled\":1529047200,\"estimated\":null,\"actual\":null},\"ga\":false,\"flightStatus\":\"\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180615/0510Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"C30\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A320\",\"aircraftTypeFriendly\":\"Airbus A320 (双发)\",\"flightId\":\"CCA1219-1528780200-schedule-0001\",\"takeoffTimes\":{\"scheduled\":1528953000,\"estimated\":1528953600,\"actual\":1528953600},\"landingTimes\":{\"scheduled\":1528959600,\"estimated\":1528959000,\"actual\":1528959000},\"gateDepartureTimes\":{\"scheduled\":1528953000,\"estimated\":null,\"actual\":1528952820},\"gateArrivalTimes\":{\"scheduled\":1528960800,\"estimated\":1528960620,\"actual\":1528959600},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180614/0510Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"C35\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A320\",\"aircraftTypeFriendly\":\"Airbus A320 (双发)\",\"flightId\":\"CCA1219-1528694029-airline-0180\",\"takeoffTimes\":{\"scheduled\":1528867200,\"estimated\":1528884840,\"actual\":1528884840},\"landingTimes\":{\"scheduled\":1528874340,\"estimated\":1528890120,\"actual\":1528890120},\"gateDepartureTimes\":{\"scheduled\":1528866600,\"estimated\":1528881600,\"actual\":1528883700},\"gateArrivalTimes\":{\"scheduled\":1528874400,\"estimated\":1528891500,\"actual\":1528890720},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180613/0520Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"D4\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A320\",\"aircraftTypeFriendly\":\"Airbus A320 (双发)\",\"flightId\":\"CCA1219-1528521261-airline-0127\",\"takeoffTimes\":{\"scheduled\":1528694400,\"estimated\":1528693800,\"actual\":1528693800},\"landingTimes\":{\"scheduled\":1528701000,\"estimated\":1528698900,\"actual\":1528698900},\"gateDepartureTimes\":{\"scheduled\":1528693800,\"estimated\":null,\"actual\":1528693440},\"gateArrivalTimes\":{\"scheduled\":1528701600,\"estimated\":1528701240,\"actual\":1528699500},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180611/0520Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"C21\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A320\",\"aircraftTypeFriendly\":\"Airbus A320 (双发)\",\"flightId\":\"CCA1219-1528434746-airline-0115:0\",\"takeoffTimes\":{\"scheduled\":1528607400,\"estimated\":1528608900,\"actual\":1528608900},\"landingTimes\":{\"scheduled\":1528612980,\"estimated\":1528613760,\"actual\":1528613760},\"gateDepartureTimes\":{\"scheduled\":1528607400,\"estimated\":null,\"actual\":1528607280},\"gateArrivalTimes\":{\"scheduled\":1528615200,\"estimated\":1528615080,\"actual\":1528614360},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180610/0510Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"C26\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"B738\",\"aircraftTypeFriendly\":\"Boeing 737-800 (双发)\",\"flightId\":\"CCA1219-1528348219-airline-0281\",\"takeoffTimes\":{\"scheduled\":1528521600,\"estimated\":1528523640,\"actual\":1528523640},\"landingTimes\":{\"scheduled\":1528528200,\"estimated\":1528529520,\"actual\":1528529520},\"gateDepartureTimes\":{\"scheduled\":1528521000,\"estimated\":null,\"actual\":1528521240},\"gateArrivalTimes\":{\"scheduled\":1528528800,\"estimated\":1528529040,\"actual\":1528530180},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180609/0520Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"D14\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A320\",\"aircraftTypeFriendly\":\"Airbus A320 (双发)\",\"flightId\":\"CCA1219-1528262044-airline-0044\",\"takeoffTimes\":{\"scheduled\":1528435200,\"estimated\":1528435860,\"actual\":1528435860},\"landingTimes\":{\"scheduled\":1528441800,\"estimated\":1528441440,\"actual\":1528441440},\"gateDepartureTimes\":{\"scheduled\":1528434600,\"estimated\":null,\"actual\":1528434000},\"gateArrivalTimes\":{\"scheduled\":1528442400,\"estimated\":1528441800,\"actual\":1528442100},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180608/0520Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"D14\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A320\",\"aircraftTypeFriendly\":\"Airbus A320 (双发)\",\"flightId\":\"CCA1219-1528175513-airline-0082\",\"takeoffTimes\":{\"scheduled\":1528348800,\"estimated\":1528349460,\"actual\":1528349460},\"landingTimes\":{\"scheduled\":1528355400,\"estimated\":1528355220,\"actual\":1528355220},\"gateDepartureTimes\":{\"scheduled\":1528348200,\"estimated\":null,\"actual\":1528348320},\"gateArrivalTimes\":{\"scheduled\":1528356000,\"estimated\":1528356120,\"actual\":1528355820},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180607/0520Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"C18\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A320\",\"aircraftTypeFriendly\":\"Airbus A320 (双发)\",\"flightId\":\"CCA1219-1528089272-airline-0108\",\"takeoffTimes\":{\"scheduled\":1528262400,\"estimated\":1528262580,\"actual\":1528262580},\"landingTimes\":{\"scheduled\":1528269000,\"estimated\":1528268340,\"actual\":1528268340},\"gateDepartureTimes\":{\"scheduled\":1528261800,\"estimated\":null,\"actual\":1528261680},\"gateArrivalTimes\":{\"scheduled\":1528269600,\"estimated\":1528269480,\"actual\":1528268940},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180606/0520Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"C35\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"B738\",\"aircraftTypeFriendly\":\"Boeing 737-800 (双发)\",\"flightId\":\"CCA1219-1528002683-airline-0002\",\"takeoffTimes\":{\"scheduled\":1528176000,\"estimated\":1528176240,\"actual\":1528176240},\"landingTimes\":{\"scheduled\":1528182600,\"estimated\":1528182780,\"actual\":1528176240},\"gateDepartureTimes\":{\"scheduled\":1528175400,\"estimated\":null,\"actual\":1528175580},\"gateArrivalTimes\":{\"scheduled\":1528183200,\"estimated\":1528178040,\"actual\":null},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":true,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180605/0520Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"C5\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"B738\",\"aircraftTypeFriendly\":\"Boeing 737-800 (双发)\",\"flightId\":\"CCA1219-1527916338-airline-0080\",\"takeoffTimes\":{\"scheduled\":1528089600,\"estimated\":1528091100,\"actual\":1528091160},\"landingTimes\":{\"scheduled\":1528096200,\"estimated\":1528097760,\"actual\":1528091160},\"gateDepartureTimes\":{\"scheduled\":1528089000,\"estimated\":null,\"actual\":1528089480},\"gateArrivalTimes\":{\"scheduled\":1528096800,\"estimated\":1528092960,\"actual\":null},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":true,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180604/0520Z/ZBAA/ZLIC\"},{\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"D9\",\"terminal\":\"3\",\"delays\":null},\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"aircraftType\":\"A320\",\"aircraftTypeFriendly\":\"Airbus A320 (双发)\",\"flightId\":\"CCA1219-1527829800-schedule-0000\",\"takeoffTimes\":{\"scheduled\":1528002600,\"estimated\":1528002780,\"actual\":1528002780},\"landingTimes\":{\"scheduled\":1528009200,\"estimated\":1528008300,\"actual\":1528008300},\"gateDepartureTimes\":{\"scheduled\":1528002600,\"estimated\":null,\"actual\":1528002180},\"gateArrivalTimes\":{\"scheduled\":1528010400,\"estimated\":1528009980,\"actual\":1528008900},\"ga\":false,\"flightStatus\":\"arrived\",\"cancelled\":false,\"resultUnknown\":false,\"diverted\":false,\"adhoc\":false,\"fruOverride\":false,\"timestamp\":null,\"permaLink\":\"/live/flight/CCA1219/history/20180603/0510Z/ZBAA/ZLIC\"}],\"additionalLogRowsAvailable\":false},\"adhoc\":false,\"adhocAvailable\":false,\"aircraft\":{\"type\":\"B738\",\"tail\":null,\"owner\":null,\"ownerLocation\":null,\"owner_type\":null,\"canMessage\":false,\"lifeguard\":false,\"heavy\":false,\"friendlyType\":\"Boeing 737-800 (双发)\"},\"airline\":{\"fullName\":\"Air China\",\"shortName\":\"Air China\",\"icao\":\"CCA\",\"iata\":\"CA\",\"callsign\":\"Air China\",\"url\":\"http://www.airchina.com.cn/\"},\"altitude\":null,\"altitudeChange\":null,\"atcIdent\":null,\"averageDelays\":{\"departure\":1350,\"arrival\":-106},\"blocked\":false,\"blockedForUser\":false,\"blockMessage\":null,\"cabinInfo\":{\"text\":\"商务舱 / 经济舱:&nbsp;热餐\",\"links\":null},\"cancelled\":false,\"codeShare\":{\"ident\":\"CCA1219\",\"displayIdent\":\"CCA1219\",\"iataIdent\":\"CA1219\",\"airline\":{\"fullName\":\"Air China\",\"shortName\":\"Air China\",\"icao\":\"CCA\",\"iata\":\"CA\",\"callsign\":\"Air China\",\"url\":\"http://www.airchina.com.cn/\"},\"friendlyIdent\":\"Air China 1219\",\"thumbnail\":{\"imageUrl\":\"https://e1.flightcdn.com/images/airline_logos/90p/CCA.png\",\"linkUrl\":\"/live/fleet/CCA\"},\"links\":{\"permanent\":\"/live/flight/CCA1219/history/20180217/0525Z/ZBAA/ZLIC\",\"staticMap\":\"/ajax/flight/map/CCA1219/20180217/0525Z/ZBAA/ZLIC/\",\"trackLog\":\"/live/flight/CCA1219/history/20180217/0525Z/ZBAA/ZLIC/tracklog\",\"flightHistory\":\"/live/flight/CCA1219/history\",\"buyFlightHistory\":\"/live/flight/CCA1219/history/buy\",\"reportInaccuracies\":\"/live/report/CCA1219/history/20180217/0525Z/ZBAA/ZLIC\",\"facebook\":\"https://facebook.com/sharer.php?u=https://zh.flightaware.com/live/flight/CCA1219/history/20180217/0525Z/ZBAA/ZLIC\",\"twitter\":\"https://twitter.com/intent/tweet?url=https://zh.flightaware.com/live/flight/CCA1219/history/20180217/0525Z/ZBAA/ZLIC&via=FlightAware\"}},\"coord\":null,\"destination\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"INC\",\"iata\":\"INC\",\"friendlyName\":\"Yinchuan Helanshan\",\"friendlyLocation\":\"宁夏 银川 CN\",\"coord\":[106.3933,38.3228],\"icao\":\"ZLIC\",\"gate\":null,\"terminal\":null,\"delays\":null},\"displayIdent\":\"CCA1219\",\"distance\":{\"elapsed\":486,\"remaining\":0,\"actual\":0},\"diverted\":false,\"flightId\":\"CCA1219-1518672300-schedule-0001\",\"flightPlan\":{\"speed\":224,\"altitude\":null,\"route\":\"\",\"directDistance\":486,\"plannedDistance\":null,\"departure\":1518845100,\"ete\":3720,\"fuelBurn\":{\"gallons\":1500,\"pounds\":10000}},\"flightStatus\":\"arrived\",\"friendlyIdent\":\"Air China 1219\",\"fruOverride\":false,\"ga\":false,\"gateArrivalTimes\":{\"scheduled\":1518852900,\"estimated\":1518853500,\"actual\":1518852360},\"gateDepartureTimes\":{\"scheduled\":1518845100,\"estimated\":null,\"actual\":1518845700},\"groundspeed\":null,\"heading\":null,\"historical\":true,\"iataIdent\":\"CA1219\",\"icon\":\"airliner\",\"ident\":\"CCA1219\",\"inboundFlight\":null,\"internal\":null,\"landingTimes\":{\"scheduled\":1518848820,\"estimated\":1518851760,\"actual\":1518850380},\"links\":{\"permanent\":\"/live/flight/CCA1219/history/20180217/0525Z/ZBAA/ZLIC\",\"staticMap\":\"/ajax/flight/map/CCA1219/20180217/0525Z/ZBAA/ZLIC/\",\"trackLog\":\"/live/flight/CCA1219/history/20180217/0525Z/ZBAA/ZLIC/tracklog\",\"flightHistory\":\"/live/flight/CCA1219/history\",\"buyFlightHistory\":\"/live/flight/CCA1219/history/buy\",\"reportInaccuracies\":\"/live/report/CCA1219/history/20180217/0525Z/ZBAA/ZLIC\",\"facebook\":\"https://facebook.com/sharer.php?u=https://zh.flightaware.com/live/flight/CCA1219/history/20180217/0525Z/ZBAA/ZLIC\",\"twitter\":\"https://twitter.com/intent/tweet?url=https://zh.flightaware.com/live/flight/CCA1219/history/20180217/0525Z/ZBAA/ZLIC&via=FlightAware\"},\"myAlerts\":{\"editAlert\":\"\",\"advancedAlert\":\"/account/manage/alerts/0/add?m_ident=CCA1219;date_start=2018-02-17;date_end=2018-02-17\"},\"myFlightAware\":null,\"origin\":{\"TZ\":\":Asia/Shanghai\",\"isValidAirportCode\":true,\"altIdent\":\"PEK\",\"iata\":\"PEK\",\"friendlyName\":\"Beijing Capital Int\\'l\",\"friendlyLocation\":\"北京 北京首都 CN\",\"coord\":[116.5846,40.0801],\"icao\":\"ZBAA\",\"gate\":\"C27\",\"terminal\":\"3\",\"delays\":null},\"publiclyVisible\":true,\"relatedThumbnails\":[{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/a1370b079083f2eac9bf561e8d29f207493d6efb\",\"target\":\"/photos/view/5534070-22a9d6dbb0e7ec0cc136635b83430f82d2aa43f4\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/6b00543b71e2099c0128d94fdf9a6031ee70737e\",\"target\":\"/photos/view/4232981-8b3fa1230a75bc83c668a4a576550c228be4c35c\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/e7255e0cf89e35e7358a5ea4592a66feb1ceca1d\",\"target\":\"/photos/view/5327636-57fdfa813efb07cf87cd61108c5cc6cc6dcf6b00\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/22cd5fa0465865e758e618fb19313e6a98ddaccc\",\"target\":\"/photos/view/4960539-b4db5ce1161d6d817eb5dc542f878e3612a86ee4\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/ff04bc8b003b75c8dbd378a7c70323aaaa299771\",\"target\":\"/photos/view/5534070-aea651bc7e92cc0f27d19aefb11db6003e288d74\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/33022292fc5ab9debc274b56ff3f5c58dcf6a3fa\",\"target\":\"/photos/view/1095011-3e1694d643a92a45464dfd48992f974ab5e19fb0\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/6148bb925a7fff0ea8bf79d8fcd7202fc6d33fd9\",\"target\":\"/photos/view/1382882-ba30b6b6f46006514f65a5b1eb1f6f3f3b8af478\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/cd610af43f6ec4f1fadaa1065b377e7fdb0083dd\",\"target\":\"/photos/view/2015486-36573531a3f5cf22268ef576fdf62eae2bc0e123\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/85327749a15222fd9a76deb3fa491d7c0d2a5aaa\",\"target\":\"/photos/view/2015486-0576175ca920e48db84edea118894ca2bdfbeaf9\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/b3d0a4260bbda2b9b86a1ba00b779744cc5c0ca2\",\"target\":\"/photos/view/2015486-d0a2873ea019fb0cfb7165dc880ccf675bcfce3c\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/4fa646df3bd1bb4cf45943362d9369cea8e46df8\",\"target\":\"/photos/view/2015486-363eccafd5b66dc37d89f2790d3d4496ebc2c58e\"},{\"thumbnail\":\"https://photos-e1.flightcdn.com/photos/retriever/4a53c40601a3f7967b1f529d06a2feaa2a988d5d\",\"target\":\"/photos/view/2015486-fe5238e64e122ebe24b54dec3c78b58eefb083bf\"}],\"remarks\":null,\"resultUnknown\":false,\"takeoffTimes\":{\"scheduled\":1518845100,\"estimated\":1518846060,\"actual\":1518846060},\"thumbnail\":{\"imageUrl\":\"https://e1.flightcdn.com/images/airline_logos/90p/CCA.png\",\"linkUrl\":\"/live/fleet/CCA\"},\"timestamp\":1518851341,\"track\":[{\"timestamp\":1518846239,\"coord\":[116.3123,40.0457],\"alt\":0,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518846415,\"coord\":[115.9400,39.9975],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518846591,\"coord\":[115.5682,39.9482],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518846767,\"coord\":[115.1970,39.8977],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518846943,\"coord\":[114.8263,39.8460],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518847119,\"coord\":[114.4562,39.7932],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518847295,\"coord\":[114.0866,39.7391],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518847471,\"coord\":[113.7177,39.6839],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518847646,\"coord\":[113.3493,39.6276],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518847822,\"coord\":[112.9815,39.5700],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518847998,\"coord\":[112.6144,39.5113],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518848174,\"coord\":[112.2479,39.4515],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518848350,\"coord\":[111.8820,39.3905],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518848526,\"coord\":[111.5167,39.3284],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518848702,\"coord\":[111.1521,39.2651],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518848878,\"coord\":[110.7882,39.2007],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518849054,\"coord\":[110.4250,39.1351],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518849230,\"coord\":[110.0624,39.0685],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518849406,\"coord\":[109.7005,39.0007],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518849582,\"coord\":[109.3393,38.9318],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518849758,\"coord\":[108.9789,38.8618],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518849934,\"coord\":[108.6191,38.7907],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518850109,\"coord\":[108.2601,38.7184],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518850285,\"coord\":[107.9017,38.6451],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518850461,\"coord\":[107.5442,38.5707],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518850637,\"coord\":[107.1874,38.4952],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518850813,\"coord\":[106.8313,38.4187],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518850989,\"coord\":[106.4760,38.3410],\"alt\":null,\"gs\":null,\"type\":\"TP\",\"isolated\":false},{\"timestamp\":1518851341,\"coord\":[106.3933,38.3228],\"alt\":0,\"gs\":null,\"type\":\"TP\",\"isolated\":false}],\"updateType\":\"\",\"waypoints\":[]}}}',\n",
       " 'status': 'arrived',\n",
       " 'takeoff_a': 1518846060,\n",
       " 'takeoff_e': 1518846060,\n",
       " 'takeoff_s': 1518845100}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# function\n",
    "def parse_flight(url):\n",
    "    r= requests.get(url, headers=headers)\n",
    "    html= r.text\n",
    "    start= html.find('var trackpollBootstrap')+len('var trackpollBootstrap')+3\n",
    "    end = html[start:].find('</script>')\n",
    "    raw_data= html[start: start+end-1]\n",
    "    json_data= json.loads(raw_data)\n",
    "    for k in json_data['flights'].keys():\n",
    "        data={}\n",
    "        \n",
    "        flight_data= json_data['flights'][k]\n",
    "        data['cancelled']=flight_data['cancelled']\n",
    "        data['status']= flight_data['flightStatus']\n",
    "        data['takeoff_s']= flight_data['takeoffTimes']['scheduled']\n",
    "        data['takeoff_e']= flight_data['takeoffTimes']['estimated']\n",
    "        data['takeoff_a']= flight_data['takeoffTimes']['actual']\n",
    "        data['landing_s']= flight_data['landingTimes']['scheduled']\n",
    "        data['landing_e']= flight_data['landingTimes']['estimated']\n",
    "        data['landing_a']= flight_data['landingTimes']['actual']\n",
    "        data['raw']= raw_data\n",
    "        \n",
    "        return data\n",
    "    \n",
    "parse_flight(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save to database\n",
    "import mysql.connector\n",
    "\n",
    "def save_flight(flight_id, data):\n",
    "    \n",
    "    cnx = mysql.connector.connect(user='flightdata', password='flightdata',\n",
    "                                  host='127.0.0.1',\n",
    "                                  database='flightdata')\n",
    "    cursor = cnx.cursor()\n",
    "    \n",
    "    update_flight = \"update  flight \\\n",
    "                  set raw_data= %s , cancelled=%s, status=%s, \\\n",
    "                  takeoff_s=%s, takeoff_e=%s,takeoff_a=%s, \\\n",
    "                    landing_s=%s, landing_e=%s,landing_a=%s \\\n",
    "                where id=%s\"\n",
    "   \n",
    "    cursor.execute(update_flight, (data['raw'],data['cancelled'], data['status'],\n",
    "                                   data['takeoff_s'], data['takeoff_e'], data['takeoff_a'],\n",
    "                                   data['landing_s'], data['landing_e'], data['landing_a'],flight_id))\n",
    "\n",
    "    cnx.commit()\n",
    "\n",
    "    cursor.close()\n",
    "    cnx.close()\n",
    "    \n",
    "data= {'cancelled': False,\n",
    " 'landing_a': 1518850380,\n",
    " 'landing_e': 1518851760,\n",
    " 'landing_s': 1518848820,\n",
    " 'raw': '{\"version\":\"2.0\"',\n",
    "       'status': 'arrived',\n",
    " 'takeoff_a': 1518846060,\n",
    " 'takeoff_e': 1518846060,\n",
    " 'takeoff_s': 1518845100}\n",
    "save_flight(1328, data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 /live/flight/CCA102/history/20180214/0430Z/VHHH/ZBAA\n",
      "1 /live/flight/CCA102/history/20180215/0430Z/VHHH/ZBAA\n",
      "121 /live/flight/CCA102/history/20180216/0430Z/VHHH/ZBAA\n",
      "120 /live/flight/CCA102/history/20180217/0430Z/VHHH/ZBAA\n",
      "119 /live/flight/CCA102/history/20180218/0430Z/VHHH/ZBAA\n",
      "118 /live/flight/CCA102/history/20180219/0430Z/VHHH/ZBAA\n",
      "117 /live/flight/CCA102/history/20180220/0430Z/VHHH/ZBAA\n",
      "116 /live/flight/CCA102/history/20180221/0430Z/VHHH/ZBAA\n",
      "115 /live/flight/CCA102/history/20180222/0430Z/VHHH/ZBAA\n",
      "114 /live/flight/CCA102/history/20180223/0430Z/VHHH/ZBAA\n"
     ]
    }
   ],
   "source": [
    "# 获取所有\n",
    "cnx = mysql.connector.connect(user='flightdata', password='flightdata',\n",
    "                              host='127.0.0.1',\n",
    "                              database='flightdata')\n",
    "cursor = cnx.cursor()\n",
    "cursor.execute(\"select id,url from flight limit 10\")\n",
    "for (id, url) in cursor:\n",
    "    print (id,url)\n",
    "    url= 'https://zh.flightaware.com' +url\n",
    "    data= parse_flight(url)\n",
    "    save_flight(id, data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20180214'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url='http://zh.flightaware.com/live/flight/CCA102/history/20180214/0430Z/VHHH/ZBAA'\n",
    "url.split('/')[7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
